{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from tqdm import tqdm\n",
    "from joblib import Parallel, delayed\n",
    "import csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_data_path = '../data/testData0626.csv'\n",
    "route_order_folder_path = '../data/route_order_data'\n",
    "port_path = '../data/port.csv'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['CNYTN-MXZLO', 'CNSHK-MYTPP', 'CNSHK-SGSIN', 'CNYTN-PAONX',\n",
       "       'CNSHK-CLVAP', 'CNYTN-ARENA', 'CNYTN-MATNG', 'CNSHK-GRPIR',\n",
       "       'CNSHK-PKQCT', 'CNSHK-SIKOP', 'CNYTN-MTMLA', 'CNYTN-MYTPP',\n",
       "       'CNSHK-MYPKG', 'CNYTN-CAVAN', 'CNHKG-ARBUE', 'CNNSA-GHTEM',\n",
       "       'CNNSA-NAWVB', 'HKHKG-FRFOS', 'CNSHK-INMUN', 'HONGKONG-BU',\n",
       "       'CNSHK-SGSIN-AEJEA'], dtype=object)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def format_data_type(data, mode='train'):\n",
    "    if mode=='test':\n",
    "        data['onboardDate'] = pd.to_datetime(data['onboardDate'], infer_datetime_format=True)\n",
    "        data['temp_timestamp'] = data['timestamp']\n",
    "        data['ETA'] = None\n",
    "        data['creatDate'] = None\n",
    "    data['loadingOrder'] = data['loadingOrder'].astype(str)\n",
    "    data['timestamp'] = pd.to_datetime(data['timestamp'], infer_datetime_format=True)\n",
    "    data['longitude'] = data['longitude'].astype(float)\n",
    "    data['latitude'] = data['latitude'].astype(float)\n",
    "    data['speed'] = data['speed'].astype(float)\n",
    "    data['TRANSPORT_TRACE'] = data['TRANSPORT_TRACE'].astype(str)\n",
    "    return data\n",
    "\n",
    "def get_test_data_info(path):\n",
    "    data = pd.read_csv(path) \n",
    "    test_trace_set = data['TRANSPORT_TRACE'].unique()\n",
    "    test_order_belong_to_trace = {}\n",
    "    for item in test_trace_set:\n",
    "        orders = data[data['TRANSPORT_TRACE'] == item]['loadingOrder'].unique()\n",
    "        test_order_belong_to_trace[item] = orders\n",
    "    return format_data_type(data, mode='test'), test_trace_set, test_order_belong_to_trace\n",
    "\n",
    "test_data_origin, test_trace_set, test_order_belong_to_trace = get_test_data_info(test_data_path)\n",
    "\n",
    "def get_port_info():\n",
    "    port_data = {}\n",
    "    test_port_set = set()\n",
    "    for route in test_trace_set:\n",
    "        ports = route.split('-')\n",
    "        test_port_set = set.union(test_port_set, set(ports))\n",
    "    port_data_origin = pd.read_csv(port_path)\n",
    "    for item in port_data_origin.itertuples():\n",
    "        if getattr(item, 'TRANS_NODE_NAME') in test_port_set:\n",
    "            port_data[getattr(item, 'TRANS_NODE_NAME')] = {'LONGITUDE': getattr(item, 'LONGITUDE'),'LATITUDE': getattr(item, 'LATITUDE') }\n",
    "    return port_data\n",
    "port_data = get_port_info()\n",
    "test_trace_set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from math import radians, cos, sin, asin, sqrt\n",
    "def haversine(lon1, lat1, lon2, lat2): # 经度1，纬度1，经度2，纬度2 （十进制度数）\n",
    "    # 将十进制度数转化为弧度\n",
    "    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])\n",
    "    # haversine公式\n",
    "    dlon = lon2 - lon1 \n",
    "    dlat = lat2 - lat1 \n",
    "    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2\n",
    "    c = 2 * asin(sqrt(a)) \n",
    "    r = 6371 # 地球平均半径，单位为公里\n",
    "    return c * r * 1000\n",
    "def get_train_route_order_data(route):\n",
    "    route_order_data_path = os.path.join(route_order_folder_path, \"{}.csv\".format(route))\n",
    "    data = pd.read_csv(route_order_data_path, header=None\n",
    "           , names=['loadingOrder','timestamp','longitude','latitude','speed'])\n",
    "    if (data.shape[0] == 0):\n",
    "        print(\"error == \", route)\n",
    "    data['timestamp'] = pd.to_datetime(data['timestamp'], infer_datetime_format=True)\n",
    "    return data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def handle_train_data(order, order_info_set,start_longitude,start_latitude,dest_longitude,dest_latitude):\n",
    "    order_info_set = order_info_set.reset_index(drop=True)\n",
    "    #       获取起航时间\n",
    "    start_time = order_info_set['timestamp'].min()\n",
    "    start_index = 0\n",
    "    for (index, info_item) in order_info_set.iterrows():\n",
    "        if abs(info_item['longitude']-start_longitude) < 0.3 and abs(info_item['latitude']-start_latitude) < 0.3 and info_item['speed'] > 0:\n",
    "            start_time = max(start_time, info_item['timestamp'])\n",
    "            start_index = index\n",
    "            break \n",
    "#       获取到达目的地时间，这里需要用 GPS 判断\n",
    "    end_time = order_info_set['timestamp'].max()\n",
    "    end_index = order_info_set.size-1\n",
    "    for (index, info_item) in order_info_set.iterrows():\n",
    "        if abs(info_item['longitude'] - dest_longitude) < 0.3 and abs(info_item['latitude'] - dest_latitude) < 0.3:\n",
    "            end_time = min(end_time, info_item['timestamp'])\n",
    "            end_index = index\n",
    "            break\n",
    "#         修正起点终点逆序\n",
    "    if (end_time < start_time):\n",
    "        start_time,end_time = end_time,start_time\n",
    "        start_index,end_index = end_index,start_index\n",
    "#         算出航行用时\n",
    "    total_seconds = (end_time - start_time).total_seconds()\n",
    "#         截取数据\n",
    "    order_info_set = order_info_set[start_index:end_index+1]\n",
    "    if (order_info_set.shape[0] > 100):\n",
    "        index = np.linspace(0, order_info_set.shape[0]-1, num=100,dtype=int).tolist()\n",
    "        order_info_set = order_info_set.iloc[index]     \n",
    "#         得到速度中位数、最值、航行距离\n",
    "    speed_median = order_info_set['speed'].median()\n",
    "    \n",
    "    total_dis = 0\n",
    "    for i in range(1, order_info_set.shape[0]):\n",
    "        total_dis = total_dis + haversine(order_info_set.iat[i-1,2],order_info_set.iat[i-1,3],\n",
    "                                          order_info_set.iat[i,2],order_info_set.iat[i,3])\n",
    "\n",
    "    feature_temp = pd.DataFrame({'loadingOrder':[order], 'speed_median':[speed_median], \n",
    "        'total_dis':[total_dis], 'label':[total_seconds]})\n",
    "\n",
    "    return feature_temp   \n",
    "\n",
    "def get_train_data(route_order_info, route,start_longitude,start_latitude,dest_longitude,dest_latitude):\n",
    "    order_list = route_order_info['loadingOrder'].unique()\n",
    "    print(route, order_list.shape)\n",
    "    \n",
    "    data_grouped = route_order_info.groupby('loadingOrder')\n",
    "\n",
    "    train_data = Parallel(n_jobs=8)(delayed(handle_train_data)\n",
    "                                    (order, group,start_longitude,start_latitude,dest_longitude,dest_latitude)\n",
    "                                    for order, group in data_grouped)\n",
    "#     for name, group in tqdm(data_grouped):\n",
    "#         handle_train_data(group,start_longitude,start_latitude,dest_longitude,dest_latitude)\n",
    "#         break\n",
    "    train_data = pd.concat(train_data)\n",
    "    return train_data.reset_index(drop=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0%|          | 0/21 [00:00<?, ?it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CNYTN-MXZLO (824,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  5%|▍         | 1/21 [04:55<1:38:28, 295.43s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       loadingOrder  speed_median     total_dis      label\n",
      "0    AA191175561416          35.0  1.486761e+07  2167517.0\n",
      "1    AA849985976383          22.0  1.439630e+07  2494699.0\n",
      "2    AH861147242321          28.0  1.445951e+07  2029634.0\n",
      "3    AL979269865349          28.0  1.461749e+07  2063997.0\n",
      "4    AM967786650823          26.0  1.462739e+07  2147925.0\n",
      "..              ...           ...           ...        ...\n",
      "819  ZW587095293016           0.0  1.433048e+07  1903686.0\n",
      "820  ZX198466828718          26.0  1.468295e+07  2024710.0\n",
      "821  ZX476649622217          26.0  1.462831e+07  2147925.0\n",
      "822  ZX741696557865          34.5  1.487163e+07  2061171.0\n",
      "823  ZY989876486292          33.0  1.441676e+07  2157238.0\n",
      "\n",
      "[824 rows x 4 columns]\n",
      "CNSHK-MYTPP (7577,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 10%|▉         | 2/21 [22:39<2:46:34, 526.05s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        loadingOrder  speed_median     total_dis     label\n",
      "0     AA236935066990          24.0  2.685929e+06  374428.0\n",
      "1     AA597788695392          33.0  2.691566e+06  288172.0\n",
      "2     AA642567891255          23.0  2.684912e+06  326548.0\n",
      "3     AA681773043065          29.0  2.720285e+06  321478.0\n",
      "4     AB283635056094          30.5  2.713435e+06  309252.0\n",
      "...              ...           ...           ...       ...\n",
      "7572  ZZ936085205111          27.0  2.700691e+06  329922.0\n",
      "7573  ZZ958783621355          29.0  2.671521e+06  290940.0\n",
      "7574  ZZ964257538884          18.0  2.713565e+06  434727.0\n",
      "7575  ZZ968915097734          22.0  2.645090e+06  326628.0\n",
      "7576  ZZ992682575994          30.0  2.711504e+06  301117.0\n",
      "\n",
      "[7577 rows x 4 columns]\n",
      "CNSHK-SGSIN (7581,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 14%|█▍        | 3/21 [39:15<3:20:04, 666.89s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        loadingOrder  speed_median     total_dis     label\n",
      "0     AA236935066990          24.0  2.668233e+06  371889.0\n",
      "1     AA597788695392          33.0  2.667065e+06  286252.0\n",
      "2     AA642567891255          23.0  2.667188e+06  321759.0\n",
      "3     AA681773043065          29.0  2.697736e+06  318936.0\n",
      "4     AB283635056094          30.5  2.695056e+06  306631.0\n",
      "...              ...           ...           ...       ...\n",
      "7576  ZZ936085205111          27.0  2.682246e+06  326712.0\n",
      "7577  ZZ958783621355          31.0  2.652040e+06  285551.0\n",
      "7578  ZZ964257538884          18.0  2.696090e+06  430896.0\n",
      "7579  ZZ968915097734          23.0  2.626224e+06  322247.0\n",
      "7580  ZZ992682575994          30.0  2.689988e+06  298753.0\n",
      "\n",
      "[7581 rows x 4 columns]\n",
      "CNYTN-PAONX (528,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 19%|█▉        | 4/21 [42:59<2:31:19, 534.09s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       loadingOrder  speed_median     total_dis      label\n",
      "0    AD337174254498          36.0  1.748102e+07  1982821.0\n",
      "1    AE414406255230          33.0  1.753927e+07  2110162.0\n",
      "2    AF291281245513          37.0  1.753676e+07  1747886.0\n",
      "3    AG172089289690          35.0  1.701965e+07  1816918.0\n",
      "4    AI695744190230          36.0  1.712188e+07  1757343.0\n",
      "..              ...           ...           ...        ...\n",
      "523  ZU726932267888          35.0  1.743891e+07  1807695.0\n",
      "524  ZV682004760041          36.0  1.701269e+07  1999384.0\n",
      "525  ZY333350082535          35.0  1.717674e+07  2142547.0\n",
      "526  ZY837282165067          35.0  1.722944e+07  1826802.0\n",
      "527  ZY846318851530          32.0  1.729784e+07  2063743.0\n",
      "\n",
      "[528 rows x 4 columns]\n",
      "CNSHK-CLVAP (69,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 24%|██▍       | 5/21 [43:32<1:42:20, 383.79s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      loadingOrder  speed_median     total_dis      label\n",
      "0   AC118422359182          31.0  1.892202e+07  3699983.0\n",
      "1   AC458999327091          32.0  1.951035e+07  2399120.0\n",
      "2   AP254924452907          32.0  1.982125e+07  2463805.0\n",
      "3   AS953078762857          27.0  2.346280e+07  3576521.0\n",
      "4   BH140084651120          29.0  1.946977e+07  2487256.0\n",
      "..             ...           ...           ...        ...\n",
      "64  YV465701899809          29.0  1.945584e+07  2504119.0\n",
      "65  YW391046018891          32.0  1.950300e+07  2563344.0\n",
      "66  YW638325217174          36.0  2.038401e+07  2367182.0\n",
      "67  ZM381066091094          28.0  1.909797e+07  2352888.0\n",
      "68  ZU416619710839          28.0  1.909797e+07  2352888.0\n",
      "\n",
      "[69 rows x 4 columns]\n",
      "CNYTN-ARENA (82,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 29%|██▊       | 6/21 [44:30<1:11:31, 286.11s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      loadingOrder  speed_median     total_dis      label\n",
      "0   AQ889642404042          30.0  2.103271e+07  3429164.0\n",
      "1   AU510504184664          24.0  2.053756e+07  3245168.0\n",
      "2   BQ425517215925          26.5  2.144197e+07  2836365.0\n",
      "3   BT552005629187          21.5  2.133688e+07  3897303.0\n",
      "4   BX633511112869          30.0  2.103300e+07  3429164.0\n",
      "..             ...           ...           ...        ...\n",
      "77  YY314907990848          29.0  2.093842e+07  3106090.0\n",
      "78  ZB984347982509          25.0  2.127990e+07  3153223.0\n",
      "79  ZK570930733133          19.0  2.140301e+07  3035161.0\n",
      "80  ZL336113063705          15.0  2.068156e+07  3180989.0\n",
      "81  ZS104404854005          29.5  2.132682e+07  3265432.0\n",
      "\n",
      "[82 rows x 4 columns]\n",
      "CNYTN-MATNG (1917,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 33%|███▎      | 7/21 [53:56<1:26:22, 370.18s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        loadingOrder  speed_median     total_dis      label\n",
      "0     AA236935066990          31.0  1.559747e+07  2454467.0\n",
      "1     AB232279245750          33.0  1.558396e+07  1711401.0\n",
      "2     AB341124011989          31.0  1.557420e+07  2418080.0\n",
      "3     AB470160617552          23.0  1.531056e+07  1728144.0\n",
      "4     AB638889966402          26.0  1.559353e+07  1682768.0\n",
      "...              ...           ...           ...        ...\n",
      "1912  ZY608485028767          35.0  1.424975e+07  1697265.0\n",
      "1913  ZZ116104459754           0.0  1.568391e+07  1765816.0\n",
      "1914  ZZ145330853506          34.0  1.538930e+07  1831166.0\n",
      "1915  ZZ471173220102          32.5  1.565470e+07  1768336.0\n",
      "1916  ZZ699919644249          35.0  1.424975e+07  1697265.0\n",
      "\n",
      "[1917 rows x 4 columns]\n",
      "CNSHK-GRPIR (115,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 38%|███▊      | 8/21 [54:32<58:28, 269.86s/it]  "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       loadingOrder  speed_median     total_dis      label\n",
      "0    AG781637728507          31.0  1.315032e+07  1635002.0\n",
      "1    AO393647206772          31.0  1.322626e+07  1772744.0\n",
      "2    AS316641737557          29.0  1.318936e+07  1711057.0\n",
      "3    AX514253100826          24.5  1.337384e+07  2284327.0\n",
      "4    AZ675353940464          30.0  1.308141e+07  1638932.0\n",
      "..              ...           ...           ...        ...\n",
      "110  YM295763629909           0.0  1.390017e+07  2004587.0\n",
      "111  YY214900937023          29.0  1.318936e+07  1711057.0\n",
      "112  ZL826451484122          27.5  1.303495e+07  1788581.0\n",
      "113  ZM490479048723          30.0  1.302213e+07  1743255.0\n",
      "114  ZZ580809707648          29.0  1.318785e+07  1711057.0\n",
      "\n",
      "[115 rows x 4 columns]\n",
      "CNSHK-PKQCT (315,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 43%|████▎     | 9/21 [55:18<40:32, 202.70s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       loadingOrder  speed_median     total_dis      label\n",
      "0    AC805998752079          14.5  8.246658e+06  1500319.0\n",
      "1    AE338443959265           4.0  8.275872e+06  1431954.0\n",
      "2    AF269374337927          30.0  8.259018e+06  1299918.0\n",
      "3    AO368338891545          11.0  8.357387e+06  1771231.0\n",
      "4    AR152962358295          23.0  8.189167e+06  1596072.0\n",
      "..              ...           ...           ...        ...\n",
      "310  ZV202516667720          30.0  8.261669e+06  1299556.0\n",
      "311  ZV381634064028           0.0  7.532922e+06  1271676.0\n",
      "312  ZY478921403623          30.0  8.259018e+06  1299918.0\n",
      "313  ZZ786612239899          15.5  8.228440e+06  1319582.0\n",
      "314  ZZ968915097734          13.5  8.163535e+06  1838054.0\n",
      "\n",
      "[315 rows x 4 columns]\n",
      "CNSHK-SIKOP (555,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 48%|████▊     | 10/21 [57:47<34:11, 186.52s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       loadingOrder  speed_median     total_dis      label\n",
      "0    AC357556396608          31.0  1.501780e+07  1978450.0\n",
      "1    AC449966542855          27.0  1.516670e+07  1971427.0\n",
      "2    AJ769380659400          31.0  1.510014e+07  1972067.0\n",
      "3    AK317742944760          25.0  1.498058e+07  1915099.0\n",
      "4    AK630924839155          33.5  1.479944e+07  2073879.0\n",
      "..              ...           ...           ...        ...\n",
      "550  ZX573411912521          28.0  9.317671e+06  1939326.0\n",
      "551  ZX952139706480          25.0  1.476917e+07  2083493.0\n",
      "552  ZZ233055353548          25.0  1.499632e+07  1915099.0\n",
      "553  ZZ675551136571          28.0  1.489943e+07  2145560.0\n",
      "554  ZZ676084526882          31.5  1.428513e+07  1637154.0\n",
      "\n",
      "[555 rows x 4 columns]\n",
      "CNYTN-MTMLA (920,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 52%|█████▏    | 11/21 [1:01:16<32:12, 193.28s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       loadingOrder  speed_median     total_dis      label\n",
      "0    AB470160617552          28.0  1.783697e+07  2064228.0\n",
      "1    AB614679152621          29.0  1.356119e+07  1593304.0\n",
      "2    AC357556396608          32.0  1.367863e+07  1683736.0\n",
      "3    AC449966542855          27.0  1.380705e+07  1689134.0\n",
      "4    AC872462954823          33.0  1.367898e+07  1484596.0\n",
      "..              ...           ...           ...        ...\n",
      "915  ZY294539775651          34.0  1.371410e+07  1671978.0\n",
      "916  ZY567315914570          31.0  1.378900e+07  1644121.0\n",
      "917  ZZ233055353548          26.0  1.361717e+07  1669473.0\n",
      "918  ZZ450337373974           0.0  1.367336e+07  1548629.0\n",
      "919  ZZ544173652326           0.0  1.363191e+07  1548629.0\n",
      "\n",
      "[920 rows x 4 columns]\n",
      "CNYTN-MYTPP (9347,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 57%|█████▋    | 12/21 [1:56:05<2:48:17, 1121.94s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        loadingOrder  speed_median     total_dis     label\n",
      "0     AA236935066990          24.0  2.685929e+06  374428.0\n",
      "1     AA597788695392          33.0  2.691566e+06  288172.0\n",
      "2     AA642567891255          23.0  2.684912e+06  326548.0\n",
      "3     AA681773043065          29.0  2.720285e+06  321478.0\n",
      "4     AB232279245750          22.5  2.687860e+06  273464.0\n",
      "...              ...           ...           ...       ...\n",
      "9342  ZZ880516271852          24.5  2.698045e+06  332141.0\n",
      "9343  ZZ907366774129          29.0  2.720285e+06  321478.0\n",
      "9344  ZZ936085205111          27.0  2.700691e+06  329922.0\n",
      "9345  ZZ964257538884          18.0  2.713565e+06  434727.0\n",
      "9346  ZZ992682575994          30.0  2.711504e+06  301117.0\n",
      "\n",
      "[9347 rows x 4 columns]\n",
      "CNSHK-MYPKG (6645,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 62%|██████▏   | 13/21 [2:22:18<2:47:37, 1257.15s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        loadingOrder  speed_median     total_dis      label\n",
      "0     AA236935066990          25.0  3.048303e+06  1064428.0\n",
      "1     AA597788695392          33.0  3.023549e+06   323290.0\n",
      "2     AA642567891255          16.0  3.043121e+06   480400.0\n",
      "3     AA681773043065          28.0  3.052245e+06   366307.0\n",
      "4     AB283635056094          30.0  3.050662e+06   351123.0\n",
      "...              ...           ...           ...        ...\n",
      "6640  ZZ936085205111          28.0  3.036652e+06   374287.0\n",
      "6641  ZZ958783621355          23.0  3.016586e+06   408057.0\n",
      "6642  ZZ964257538884          17.0  3.085270e+06   555704.0\n",
      "6643  ZZ968915097734          21.0  2.998053e+06   884824.0\n",
      "6644  ZZ992682575994          15.0  3.043906e+06   381616.0\n",
      "\n",
      "[6645 rows x 4 columns]\n",
      "CNYTN-CAVAN (178,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 67%|██████▋   | 14/21 [2:23:07<1:44:24, 894.86s/it] "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       loadingOrder  speed_median     total_dis      label\n",
      "0    AC973431878181          33.0  1.094381e+07  1233780.0\n",
      "1    AL455172449176          32.0  1.091632e+07  1279323.0\n",
      "2    AM630773305515          32.0  1.091632e+07  1279323.0\n",
      "3    AY143679328090          32.5  1.099063e+07  1213693.0\n",
      "4    BF148288797241          34.0  1.087544e+07  1254068.0\n",
      "..              ...           ...           ...        ...\n",
      "173  YY850490269067          32.5  1.093447e+07  1383308.0\n",
      "174  ZB861358603487          35.0  1.088456e+07  1267935.0\n",
      "175  ZD215105078416          18.0  1.086403e+07  1259959.0\n",
      "176  ZF166170112101          36.0  1.093270e+07  1181565.0\n",
      "177  ZH603370675025          33.0  1.089017e+07  1238274.0\n",
      "\n",
      "[178 rows x 4 columns]\n",
      "CNHKG-ARBUE (83,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 71%|███████▏  | 15/21 [2:24:14<1:04:38, 646.37s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      loadingOrder  speed_median     total_dis      label\n",
      "0   AQ889642404042          30.0  2.103368e+07  3429375.0\n",
      "1   AU510504184664          24.0  2.053907e+07  3245323.0\n",
      "2   BQ425517215925          26.5  2.144451e+07  2836588.0\n",
      "3   BT552005629187          22.0  2.137377e+07  3892423.0\n",
      "4   BX633511112869          30.0  2.103418e+07  3429375.0\n",
      "..             ...           ...           ...        ...\n",
      "78  YY314907990848          29.0  2.094252e+07  3106668.0\n",
      "79  ZB984347982509          25.5  2.128172e+07  3153361.0\n",
      "80  ZK570930733133          18.0  2.140380e+07  3035341.0\n",
      "81  ZL336113063705          15.0  2.068156e+07  3180989.0\n",
      "82  ZS104404854005          29.0  2.133164e+07  3265670.0\n",
      "\n",
      "[83 rows x 4 columns]\n",
      "CNNSA-GHTEM (109,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 76%|███████▌  | 16/21 [2:25:14<39:12, 470.50s/it]  "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       loadingOrder  speed_median     total_dis      label\n",
      "0    AI556878934887          25.0  1.785755e+07  2662752.0\n",
      "1    BH268733506477          26.0  1.793127e+07  2733730.0\n",
      "2    BM492258958969          24.0  1.797361e+07  2712305.0\n",
      "3    BV204161420507          25.0  1.803788e+07  2669928.0\n",
      "4    BV408123099936          28.0  1.807284e+07  2874957.0\n",
      "..              ...           ...           ...        ...\n",
      "104  ZJ419915508236          24.0  1.770251e+07  2706137.0\n",
      "105  ZK502690532194          26.0  1.684251e+07  2886862.0\n",
      "106  ZN247404591619          26.0  1.802913e+07  3054783.0\n",
      "107  ZV464815872671          24.0  1.802975e+07  2943940.0\n",
      "108  ZY198608121827          18.0  1.789850e+07  2867100.0\n",
      "\n",
      "[109 rows x 4 columns]\n",
      "CNNSA-NAWVB (205,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 81%|████████  | 17/21 [2:26:32<23:31, 352.82s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       loadingOrder  speed_median     total_dis      label\n",
      "0    AC971525935172          30.0  1.421566e+07  2074432.0\n",
      "1    AE696573138143          27.0  1.432030e+07  2066705.0\n",
      "2    AI365973571487          28.0  1.439494e+07  2180772.0\n",
      "3    AJ183726554428          22.0  1.427273e+07  2457480.0\n",
      "4    AM102991728889          24.0  1.452708e+07  2191788.0\n",
      "..              ...           ...           ...        ...\n",
      "200  ZK351687238829          30.0  1.432640e+07  2062990.0\n",
      "201  ZS589584162034          21.5  1.431313e+07  2099506.0\n",
      "202  ZU401862054668          30.0  1.421566e+07  2074432.0\n",
      "203  ZU788257416574          25.0  1.433480e+07  2144583.0\n",
      "204  ZY357155860431          27.0  1.437756e+07  2073935.0\n",
      "\n",
      "[205 rows x 4 columns]\n",
      "HKHKG-FRFOS (80,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 86%|████████▌ | 18/21 [2:27:02<12:47, 255.95s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      loadingOrder  speed_median     total_dis      label\n",
      "0   AO393647206772          29.0  1.509231e+07  2616736.0\n",
      "1   BS141811220707          15.5  1.543088e+07  2395084.0\n",
      "2   BT719395802096          32.5  1.438635e+07  2132292.0\n",
      "3   BY440962771393          27.0  1.372644e+07  1911958.0\n",
      "4   BZ623190611735           7.5  1.560521e+07  2238686.0\n",
      "..             ...           ...           ...        ...\n",
      "75  YT360665182781          22.0  1.458550e+07  2409520.0\n",
      "76  ZB421092044630          28.5  1.635456e+07  2597349.0\n",
      "77  ZE646628437331          11.0  1.558287e+07  2201389.0\n",
      "78  ZI166705356011          25.0  1.572325e+07  2510068.0\n",
      "79  ZM490479048723          28.5  1.531528e+07  2575134.0\n",
      "\n",
      "[80 rows x 4 columns]\n",
      "CNSHK-INMUN (236,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 90%|█████████ | 19/21 [2:27:37<06:19, 189.69s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       loadingOrder  speed_median     total_dis      label\n",
      "0    AC805998752079          28.0  7.826115e+06  1088790.0\n",
      "1    AE338443959265          14.5  7.772251e+06  1090489.0\n",
      "2    AF269374337927          31.0  7.774423e+06  1020228.0\n",
      "3    AO368338891545          19.0  7.857523e+06  1439257.0\n",
      "4    AV996737351035           0.0  7.740607e+06   923455.0\n",
      "..              ...           ...           ...        ...\n",
      "231  ZJ992630155921          27.0  7.698865e+06  1053212.0\n",
      "232  ZV202516667720          31.0  7.775146e+06  1019866.0\n",
      "233  ZY478921403623          31.0  7.774423e+06  1020228.0\n",
      "234  ZZ786612239899          19.0  7.812236e+06  1150478.0\n",
      "235  ZZ968915097734          21.5  7.750055e+06  1493900.0\n",
      "\n",
      "[236 rows x 4 columns]\n",
      "HONGKONG-BU (82,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 95%|█████████▌| 20/21 [2:28:40<02:31, 151.54s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      loadingOrder  speed_median     total_dis      label\n",
      "0   AQ889642404042          30.0  2.104489e+07  3431548.0\n",
      "1   AU510504184664          24.0  2.057461e+07  3247761.0\n",
      "2   BQ425517215925          27.0  2.141703e+07  2839037.0\n",
      "3   BT552005629187          22.0  2.127967e+07  3889727.0\n",
      "4   BX633511112869          30.0  2.104522e+07  3431548.0\n",
      "..             ...           ...           ...        ...\n",
      "77  YY314907990848          29.0  2.095753e+07  3109057.0\n",
      "78  ZB984347982509          24.5  2.129936e+07  3155870.0\n",
      "79  ZK570930733133          20.0  2.158596e+07  3037870.0\n",
      "80  ZL336113063705          15.0  2.070638e+07  3183688.0\n",
      "81  ZS104404854005          29.0  2.135118e+07  3268311.0\n",
      "\n",
      "[82 rows x 4 columns]\n",
      "CNSHK-SGSIN-AEJEA (946,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 21/21 [2:30:54<00:00, 431.16s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       loadingOrder  speed_median     total_dis      label\n",
      "0    AA597788695392          32.0  9.153124e+06  1256949.0\n",
      "1    AA642567891255          21.0  9.114304e+06  1200145.0\n",
      "2    AC301230043784          29.0  9.121916e+06  1136634.0\n",
      "3    AD733551674501          29.0  9.121916e+06  1136634.0\n",
      "4    AI338314636212          29.5  9.078720e+06  1152492.0\n",
      "..              ...           ...           ...        ...\n",
      "941  ZY466646686415          22.0  9.225040e+06  1303323.0\n",
      "942  ZY717447456408          24.0  9.114551e+06  1136350.0\n",
      "943  ZY724149239691          29.0  9.021306e+06  1088948.0\n",
      "944  ZZ270424324968          29.5  9.188068e+06  1150454.0\n",
      "945  ZZ958783621355          30.0  9.118204e+06  1113821.0\n",
      "\n",
      "[946 rows x 4 columns]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "for route in tqdm(test_order_belong_to_trace):\n",
    "    ports = route.split(\"-\")\n",
    "    start_port = ports[0]\n",
    "    dest_port = ports[-1]\n",
    "    start_longitude = port_data[start_port]['LONGITUDE']\n",
    "    start_latitude = port_data[start_port]['LATITUDE']\n",
    "    dest_longitude = port_data[dest_port]['LONGITUDE']\n",
    "    dest_latitude = port_data[dest_port]['LATITUDE']\n",
    "    route_order_info = get_train_route_order_data(route)\n",
    "    train_data = get_train_data(route_order_info, route,start_longitude,start_latitude,dest_longitude,dest_latitude)\n",
    "    train_data.to_csv(os.path.join(route_order_folder_path, \"{}_speed_dis_time.csv\".format(route)))\n",
    "    print(train_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.6 64-bit ('AI': conda)",
   "language": "python",
   "name": "python37664bitaiconda6859e03b37c34f0182c9bde8073269f7"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
